<?php

use Drupal\s_commerce_service\Entity\SCommerceService;
use Drupal\s_commerce_service\Entity\SCommerceServiceLine;
use Drupal\s_commerce_service\SCommerceServiceRefundInterface;
use Drupal\store\Entity\Store;

function s_commerce_service_s_commerce_service_entity_form($form, &$form_state, SCommerceService $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 's_commerce_service') . '/s_commerce_service.forms.inc';
  $form_state[SCommerceService::class] = $e;
  $store = store_load($e->store_id);
  if ($store instanceof Store) {
    $form['store'] = [
      '#type' => 'fieldset',
      '#title' => t('店铺信息'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];
    $form['store']['row'] = [
      '#type' => 'item',
      '#prefix' => '<div class="row">',
      '#suffix' => '</div>',
    ];
    $form['store']['row']['name'] = [
      '#type' => 'textfield',
      '#title' => t('店铺名'),
      '#default_value' => $store->name,
      '#disabled' => TRUE,
      '#prefix' => '<div class="col-sm-6">',
      '#suffix' => '</div>',
    ];
    $form['store']['row']['type'] = [
      '#type' => 'textfield',
      '#title' => t('店铺类型'),
      '#default_value' => $store->getTypeOptions()[$store->type],
      '#disabled' => TRUE,
      '#prefix' => '<div class="col-sm-6">',
      '#suffix' => '</div>',
    ];
  }

  $header = [
    '订单号',
    '产品名',
    '规格',
    'SKU',
    '数量',
    '价格',
    '总价',
  ];

  $line = commerce_line_item_load($e->line_item_id);
  if ($line) {
    $lineWrapper = entity_metadata_wrapper('commerce_line_item', $line);
    $product = $lineWrapper->commerce_product->value();

    $rows = [];
    $rows[] = [
      $e->order_id,
      $product->title,
      $lineWrapper->commerce_product->field_ref_standard->name->value(),
      $product->sku,
      $line->quantity + 0,
      $lineWrapper->commerce_unit_price->amount->value() / 100 + 0,
      $lineWrapper->commerce_total->amount->value() / 100 + 0,
    ];
    $output = theme('table', [
      'header' => $header,
      'rows' => $rows,
    ]);
    $form['container']['line_item_id'] = [
      '#markup' => $output,
    ];
  }

  $form['container'] = [
    '#title' => t('售后产品'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];


  $form['type'] = [
    '#type' => 'select',
    '#title' => t('申请类型'),
    '#required' => TRUE,
    '#options' => $e->getTypeOptions(),
    '#default_value' => $e->type,
    '#chosen' => TRUE,
    '#disabled' => TRUE,
  ];
  $form['amount'] = [
    '#type' => 'textfield',
    '#title' => t('退款金额'),
    '#default_value' => $e->amount / 100,
    '#disabled' => TRUE,
  ];
  $form['amount_refund_type'] = [
    '#type' => 'select',
    '#title' => t('退款方式'),
    '#required' => TRUE,
    '#options' => $e->getAmountRefundTypeOptions(),
    '#default_value' => $e->amount_refund_type,
    '#chosen' => TRUE,
  ];
  $term = taxonomy_term_load($e->reason);
  $form['reason'] = [
    '#type' => 'textfield',
    '#title' => t('退款原因'),
    '#default_value' => !empty($term) ? $term->name : '',
    '#disabled' => TRUE,
  ];


  $form['shipping'] = [
    '#type' => 'fieldset',
    '#title' => t('退货信息'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $form['shipping']['shipping_type'] = [
    '#type' => 'textfield',
    '#title' => t('快递公司'),
    '#default_value' => $e->shipping_type,
    '#disabled' => TRUE,
  ];
  $form['shipping']['shipping_number'] = [
    '#type' => 'textfield',
    '#title' => t('快递单号'),
    '#default_value' => $e->shipping_number,
    '#disabled' => TRUE,
  ];


  $form['reason_desc'] = [
    '#title' => t('退款说明'),
    '#type' => 'textarea',
    '#default_value' => $e->reason_desc,
    '#description' => t('拒绝通过时填写理由.'),
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  //3个操作 审核 确认退货到货 退款
  if ($e->status == 0) {
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('通过'),
      '#submit' => ['s_commerce_service_entity_form_allow'],
    ];
    $form['actions']['delete'] = [
      '#type' => 'submit',
      '#value' => t('拒绝'),
      '#submit' => ['s_commerce_service_entity_form_deny'],
    ];
  }
  if ($e->status == 4) {
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('确认退货到货'),
      '#submit' => ['s_commerce_service_entity_form_confirm'],
    ];
  }
  if (in_array($e->status, [1])) {
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('退款'),
      '#submit' => ['s_commerce_service_entity_form_refund'],
    ];
  }
  return $form;
}

function s_commerce_service_entity_form_allow($form, &$form_state) {
  $orderService = $form_state[SCommerceService::class];
  if ($orderService instanceof SCommerceService) {
    $es = entity_load('s_commerce_service_line', FALSE, [
      'line_item_id' => $orderService->line_item_id,
    ]);
    if (!empty($es)) {
      $e = reset($es);
    }
    else {
      $e = new SCommerceServiceLine();
      $e->line_item_id = $orderService->line_item_id;
    }
    $orderService->status = 1;
    $e->status = 1;
    //申请类型 0 仅退款 1 退货退款
    if ($orderService->type == 1) {
      $orderService->status = 4;
      $e->status = 4;
    }
    $e->save();
    $orderService->save();
  }
}

function s_commerce_service_entity_form_deny($form, &$form_state) {
  $orderService = $form_state[SCommerceService::class];
  if ($orderService instanceof SCommerceService) {
    $es = entity_load('s_commerce_service_line', FALSE, [
      'line_item_id' => $orderService->line_item_id,
    ]);
    if (!empty($es)) {
      $e = reset($es);
    }
    else {
      $e = new SCommerceServiceLine();
      $e->line_item_id = $orderService->line_item_id;
    }
    $orderService->status = 3;
    $e->status = 3;
    $e->reason_desc = $form_state['values']['reason_desc'];
    $e->save();
    $orderService->save();
  }
}

function s_commerce_service_entity_form_confirm($form, &$form_state) {
  $orderService = $form_state[SCommerceService::class];
  if ($orderService instanceof SCommerceService) {
    $es = entity_load('s_commerce_service_line', FALSE, [
      'line_item_id' => $orderService->line_item_id,
    ]);
    if (!empty($es)) {
      $e = reset($es);
    }
    else {
      $e = new SCommerceServiceLine();
      $e->line_item_id = $orderService->line_item_id;
    }
    $e->status = 1;
    $orderService->status = 1;
    $e->save();
    $orderService->save();
  }
}

function s_commerce_service_entity_form_refund($form, &$form_state) {
  $orderService = $form_state[SCommerceService::class];
  if ($orderService instanceof SCommerceService) {
    $orderService->amount = bcmul($form_state['values']['amount'], 100);
    $orderService->amount_refund_type = $form_state['values']['amount_refund_type'];
    $orderService->status = 5;
    $orderService->save();
    $es = entity_load('s_commerce_service_line', FALSE, [
      'line_item_id' => $orderService->line_item_id,
    ]);
    if (!empty($es)) {
      $e = reset($es);
    }
    else {
      $e = new SCommerceServiceLine();
      $e->line_item_id = $orderService->line_item_id;
    }
    if ($e instanceof SCommerceServiceLine && $e->status == 1) {
      //状态 0 待审核 1 待退款 2 已完成 3 拒绝 4 待退货 5 退款处理中
      $e->status = 5;
      $e->save();
    }
    //退款需要退佣金
    youji_income_order_line_refund_qty($orderService->order_id, $orderService->line_item_id, $orderService->receipt_number);
    $controller = s_commerce_service_refund_init($orderService->amount_refund_type);
    if ($controller instanceof SCommerceServiceRefundInterface) {
      $controller->handler($orderService);
    }
  }
}


function s_commerce_service_s_commerce_service_entity_form_submit($form, &$form_state) {
  $e = $form_state[SCommerceService::class];
  if ($e instanceof SCommerceService) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function s_commerce_service_s_commerce_service_delete_form($form, &$form_state, SCommerceService $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 's_commerce_service') . '/s_commerce_service.forms.inc';
  $form_state[SCommerceService::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/s_commerce_service/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function s_commerce_service_s_commerce_service_delete_form_submit($form, &$form_state) {
  $e = $form_state[SCommerceService::class];
  if ($e instanceof SCommerceService) {
    $e->delete();
  }
}

function s_commerce_service_s_commerce_service_line_entity_form($form, &$form_state, SCommerceServiceLine $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 's_commerce_service') . '/s_commerce_service.forms.inc';
  $form_state[SCommerceServiceLine::class] = $e;

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function s_commerce_service_s_commerce_service_line_entity_form_submit($form, &$form_state) {
  $e = $form_state[SCommerceServiceLine::class];
  if ($e instanceof SCommerceServiceLine) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function s_commerce_service_s_commerce_service_line_delete_form($form, &$form_state, SCommerceServiceLine $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 's_commerce_service') . '/s_commerce_service.forms.inc';
  $form_state[SCommerceServiceLine::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/s_commerce_service_line/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function s_commerce_service_s_commerce_service_line_delete_form_submit($form, &$form_state) {
  $e = $form_state[SCommerceServiceLine::class];
  if ($e instanceof SCommerceServiceLine) {
    $e->delete();
  }
}

function s_commerce_service_clear_form($form, &$form_state) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 's_commerce_service') . '/s_commerce_service.forms.inc';
  return confirm_form($form,
    t('确定清空所有售后数据?'),
    'admin/content/s_commerce_service',
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function s_commerce_service_clear_form_submit($form, &$form_state) {
  $es = s_commerce_service_load_multiple(FALSE);
  foreach ($es as $e) {
    if ($e instanceof SCommerceService) {
      $e->delete();
    }
  }
  db_truncate('s_commerce_service_line')->execute();
  db_truncate('s_commerce_service_line_revision')->execute();
  $form_state['redirect'] = 'admin/content/s_commerce_service';
}
